package com.liziyi0914.morearmor.data;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.liziyi0914.morearmor.armor.BaseArmor;
import net.minecraft.entity.Entity;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 2021/6/27<br>
 * MoreArmor<br>
 *
 * @author huanmeng_qwq
 */
public class DataManager {
//    private static final Multimap<UUID, BaseArmor> data = LinkedHashMultimap.create(); //reason: ConcurrentModificationException
    private static final Map<UUID,BaseArmor> helmet= new ConcurrentHashMap<>();
    private static final Map<UUID,BaseArmor> chestPlate= new ConcurrentHashMap<>();
    private static final Map<UUID,BaseArmor> leg= new ConcurrentHashMap<>();
    private static final Map<UUID,BaseArmor> boot= new ConcurrentHashMap<>();

    public static Collection<BaseArmor> get(Entity entity) {
        List<BaseArmor> armors=new ArrayList<>();
        if(helmet.containsKey(entity.getUuid())){
            armors.add(helmet.get(entity.getUuid()));
        }
        if(chestPlate.containsKey(entity.getUuid())){
            armors.add(chestPlate.get(entity.getUuid()));
        }
        if(leg.containsKey(entity.getUuid())){
            armors.add(leg.get(entity.getUuid()));
        }
        if(boot.containsKey(entity.getUuid())){
            armors.add(boot.get(entity.getUuid()));
        }
        return armors;
    }

    public static void clear(Entity entity) {
        helmet.remove(entity.getUuid());
        chestPlate.remove(entity.getUuid());
        leg.remove(entity.getUuid());
        boot.remove(entity.getUuid());
    }

    public static void put(UUID uuid, BaseArmor armor) {
        switch (armor.getSlotType()) {
            case CHEST -> {
                chestPlate.put(uuid, armor);
            }
            case LEGS -> {
                leg.put(uuid, armor);
            }
            case FEET ->{
                boot.put(uuid, armor);
            }
            case HEAD -> {
                helmet.put(uuid, armor);
            }
        }
    }
}
